﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
 

namespace Music_Tag_Organizer
{
    public class Track : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;


        public TagLib.File MusicFile { get; set; }
        public System.IO.FileInfo FileInformation { get; set; }



        private String m_ContributingArtist;
        public String ContributingArtist
        {
            get { return m_ContributingArtist; }
            set
            {
                m_ContributingArtist = value;
                OnPropertyChanged("ContributingArtist");
            }
        }

        private String m_Folder;
        public String Folder
        {
            get { return m_Folder; }
            set
            {
                m_Folder = value;
                OnPropertyChanged("Folder");
            }
        }


        private String m_Title;
        public String Title
        {
            get { return m_Title; }
            set
            {
                m_Title = value;
                OnPropertyChanged("Title");
            }
        }

        private String m_Length;
        public String Length
        {
            get { return m_Length; }
            set
            {
                m_Length = value;
                OnPropertyChanged("Length");
            }
        }

        private String m_Album;
        public String Album
        {
            get { return m_Album; }
            set
            {
                m_Album = value;
                OnPropertyChanged("Album");
            }
        }

        private String m_FileName;
        public String FileName
        {
            get { return m_FileName; }
            set
            {
                m_FileName = value;
                OnPropertyChanged("FileName");
            }
        }

        private String m_SuggestedArtist;
        public String SuggestedArtist
        {
            get
            {
                return m_SuggestedArtist;
            }
            set
            {
                m_SuggestedArtist = value;
                OnPropertyChanged("SuggestedArtist");
            }
        }

        private String m_SuggestedTitle;
        public String SuggestedTitle
        {
            get
            {
                return m_SuggestedTitle;
            }
            set
            {
                m_SuggestedTitle = value;
                OnPropertyChanged("SuggestedTitle");
            }
        }

        private String m_SuggestedAlbum;
        public String SuggestedAlbum
        {
            get
            {
                return m_SuggestedAlbum;
            }
            set
            {
                m_SuggestedAlbum = value;
                OnPropertyChanged("SuggestedAlbum");
            }
        }

        public Track(String fileName)
        {
            FileInformation = new System.IO.FileInfo(fileName);

            FileName = FileInformation.Name;
            Folder = FileInformation.Directory.Name;
            Album = String.Empty;
            Title = String.Empty;
            ContributingArtist = String.Empty;
            Suggest();
        }

        /// <summary>
        /// Suggests title, artist and album based on filename
        /// </summary>
        public void Suggest()
        {
            try
            {
                // Suggest Title and Artist, remove all numbers and special chars
                String[] parts = FileName.Split('-');
                if (parts.Count() > 1)
                {
                    m_SuggestedTitle = parts[0];
                    m_SuggestedArtist = parts[1];

                }

                // Album suggestion based on folder where file resides.
                m_SuggestedAlbum = FileInformation.Directory.Name;

                // Clear any extensions
                m_SuggestedArtist = m_SuggestedArtist.Replace(".mp3", "");
                m_SuggestedArtist = m_SuggestedArtist.Replace(".wma", "");

                m_SuggestedTitle = m_SuggestedTitle.Replace(".mp3", "");
                m_SuggestedTitle = m_SuggestedTitle.Replace(".wma", "");

                // Clear numbers
                System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex("[a-zA-Z].*");

                m_SuggestedTitle = regEx.Match(m_SuggestedTitle).Value;
                m_SuggestedArtist = regEx.Match(m_SuggestedArtist).Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
            }

        } // end suggest()

        public void SwapSuggestion()
        {
            String temp = SuggestedArtist.Trim();
            SuggestedArtist = SuggestedTitle.Trim();
            SuggestedTitle = temp;
        }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        public void UseAllSuggestions()
        {
            ContributingArtist = SuggestedArtist;
            Title = SuggestedTitle;
            Album = SuggestedAlbum;
        }

        public void UseTitleSuggestion()
        {
            Title = SuggestedTitle;

        }

        public void UseArtistSuggestion()
        {
            ContributingArtist = SuggestedArtist;
        }

        public void UseArtistAndTitleSuggestion()
        {
            ContributingArtist = SuggestedArtist;
            Title = SuggestedTitle;
        }

        public void UseAlbumSuggestion()
        {
            Album = SuggestedAlbum;
        }

        public void Save()
        {
            try
            {

                // Set props back to tag
                MusicFile.Tag.Album = Album;
                String[] perfs = new String[1];
                perfs[0] = ContributingArtist;

                MusicFile.Tag.Performers = perfs;
                MusicFile.Tag.Title = Title;

                MusicFile.Save();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
            }

        } // end save

        public void Capitalize()
        {
            ContributingArtist = CapitalizeWords(ContributingArtist.ToLower());
            Title = CapitalizeWords(Title.ToLower());
        }

        public static string CapitalizeWords(string value)
        {
            return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value);
        }


    } // end class
}
